/// <reference path="sage.util.js" />
/// <reference path="sage.input.__.base.js" />

var TestObjectLocationInput = BaseInputHandler.extend({
    initialize: function initialize() {
        BaseInputHandler.initialize.call(this, []);
        this.readingCount = 0;
        this.errorCount = 0;
        this.lastError = null;
        this.lastWasError = false;
        this.geo = null;
        this.timerHandle = null;
        this.timerInterval = 1000;
        this.sampleFeed = [
            /*{ objId: 661, lat: 39.955813, lon: -86.011362 },
            { objId: 662, lat: 39.960744, lon: -86.01546 },
            { objId: 663, lat: 39.960753, lon: -86.028517 },
            { objId: 664, lat: 40.026871, lon: -86.394355 },
            { objId: 664, lat: 39.95567912657765, lon: -86.0133147239685 },
            { objId: 663, lat: 39.960512, lon: -86.02856 },
            { objId: 665, lat: 39.956142, lon: -86.039257 },
            { objId: 666, lat: 39.970549, lon: -86.034107 },
            { objId: 667, lat: 39.971273, lon: -86.004324 }*/
        
            { objId: 1, lat: 39.95567912657765, lon: -86.0133147239685 },
            { objId: 1, lat: 39.95583949628957, lon: -86.01331740617752 },
            { objId: 1, lat: 39.956028649825384, lon: -86.01332813501358 },
            { objId: 1, lat: 39.95583949628957, lon: -86.01304113864899 }];

        this.sampleFeedIndex = 0;
    },
    sendObjectCoordinates: function sendObjectCoordinates(context) {
        this.readingCount++;
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("objectlocation", context);
        }
    },
    setup: function setup() {
        if (!this.enabled) {
            this.enabled = true;
            var self = this;

            self.timerHandle = setInterval(function () {
                var location = self.sampleFeed[self.sampleFeedIndex];
                self.sampleFeedIndex++;
                if (self.sampleFeedIndex >= self.sampleFeed.length)
                    self.sampleFeedIndex = 0;

                var context = { prevent: false, oid: location.objId, latitude: location.lat, longitude: location.lon };
                self.sendObjectCoordinates(context);
            }, self.timerInterval);
        }
    },
    teardown: function teardown() {
        if (this.enabled) {
            this.enabled = false;
            clearTimeout(this.timerHandle);
        }
    }
});
